<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
</head>

<body>
    <div class="content">
        
    </div>
    <script src="jquery.min.js"></script>
    <script>
        let parent = document.querySelector(".content");
        $.ajax({
            url:"blogList",
            type:"get",
            success: function(data,status){
                console.log("进入 success 函数" + status);
                let navA = document.createElement("a");
                navA.innerHTML = "写博客";
                navA.href = "insert.html";
                parent.appendChild(navA)
                buildBlogs(data);
            },
            // error: function(data,status){
            //     console.log("进入error函数" + status);
            //     location.assign("login.html");
            // }
        })
        //根据响应中的数据,构造html
        function buildBlogs(data){
            //遍历这个data
            for(let blog of data){
                //blog就是一个json对象
                // let blogId = document.createElement("div");
                // blogId.innerHTML = blog.blogId;
                // parent.appendChild(blogId);
                let title = document.createElement("div");
                title.innerHTML = "博客标题 : " + blog.title;
                parent.appendChild(title);
                let content = document.createElement("div");
                content.innerHTML = "博客内容 : " + blog.content;
                parent.appendChild(content);
                let postTime = document.createElement("div");
                postTime.innerHTML = "创建时间 : " + getMyDate(blog.postTime);
                parent.appendChild(postTime);
                let userId = document.createElement("div");
                userId.innerHTML = "作者ID : " + blog.userId;
                parent.appendChild(userId);
                let see = document.createElement("a");
                see.innerHTML = "查看详情";
                see.href = "blogDetails.html?blogId=" + blog.blogId;
                parent.appendChild(see);
            }
        }
        function getMyDate(str){
            var oDate = new Date(str),
                oYear = oDate.getFullYear(),//年
                oMonth = oDate.getMonth()+1,//月
                oDay = oDate.getDate(),//日
                oHour = oDate.getHours(),//时
                oMin = oDate.getMinutes(),//分
                oSen = oDate.getSeconds(),//秒
                oFf=oDate.getMilliseconds()//毫秒
                oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen)+':'+getzf(oFf);//最后拼接时间
            return oTime;
        };
        //补0操作
        function getzf(num){
            if(parseInt(num) < 10){
                num = '0'+num;
            }
            return num;
        }
    </script>
</body>

</html>